package net.aaa.hotel.controller;

import net.aaa.hotel.dao.*;
import net.aaa.hotel.entity.TChangeroom;
import net.aaa.hotel.entity.TRuzhudetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("changeroom")
public class TChangeRoomController {
    @Autowired
    TChangeRoomDao changeRoomDao;
    @Autowired
    GustRoomDao gustRoomDao;
    @Autowired
    TRuzhudetailDao ruzhudetailDao;
    @Autowired
    HTypeDao typeDao;
    @Autowired
    DaSao daSao;

    @RequestMapping("add")
    @ResponseBody
    /**
     * 换房成功后要改变旧房间与新房间的状态
     */
    public int addChangeRoomRecord(TChangeroom changeroom){
        int count = changeRoomDao.getCountByRuZhuId(changeroom.getRuzhuid());
    //    System.out.println("换房数量 "+count);
        if(count>=3){
            return -1;
        }
    //    System.out.println(changeroom);
        // 旧房间状态改为待打扫
        int oldr = gustRoomDao.changeRoomStatusById(1, changeroom.getOldroomid());
        // 新房间状态改为入住
        int newr = gustRoomDao.changeRoomStatusById(2, changeroom.getNewroomid());
        // 添加换房记录
        int record = changeRoomDao.addChangeRomRecord(changeroom);
        // 改变入住详情中的订单状态
        int roomId = ruzhudetailDao.changRuZhuDeatilRoomId(changeroom.getRuzhuid(), changeroom.getNewroomid());
        return newr+oldr+record+roomId;
    }


    @RequestMapping("dasao")
    @ResponseBody
    public int addChangeRoomStatus(Integer id,Integer typeid){
        Map map = typeDao.searchType(typeid + "");
        Integer count = Integer.parseInt(map.get("count") + "");
        List<Map> mapList = daSao.getAllConsumable();
        int sy = 0 ;
        String sid = null;
        for (Map ayi : mapList) {
            int s = Integer.parseInt(ayi.get("count") + "");
            if(s>count){
                sy = s-count;
                sid = ayi.get("id") + "";
                break;
            }
        }
        if(sid==null){
            return -1;
        }
        int cun = daSao.changeKuCun(sid, sy);
        int status = gustRoomDao.changeRoomStatusById(0,id);
        return status+cun;
    }
}
